class HierarchyParser
{

	public static function read(inst : String) : List<HierarchyNode>
	{

		var allnodes : List<HierarchyNode>;	
		var lnum = 0;
		var last : HierarchyNode;

		allnodes = new List();
		
		var lines = inst.split("\n");

		for (line in lines)
		{
			if (line.length>0)
			{
				var split = line.split(":");
				var count = StringTools.trim(split[0]);
				var name = StringTools.trim(split[1]);
				
				var depth = count.length;

				var cur = new HierarchyNode(name);
				cur.depth = depth;
				allnodes.add(cur);

				if (depth==1)
				{
					// new tree
				}
				else if (depth>last.depth)
				{
					cur.parent = last;
					last.children.push(cur);
				}
				else if (depth==last.depth)
				{
					cur.parent = last.parent;
					last.parent.children.push(cur);
				}
				else
				{
					var drilldown = last;
					while (drilldown.depth>depth)
					{
						drilldown = drilldown.parent;
					}
					cur.parent = drilldown.parent;
					cur.parent.children.push(cur);
				}
				
				lnum+=1;
				last = cur;				
				
			}
		}
					
		//trace(".....");

		//allnodes.first().printchildren();
		
		return allnodes;
	
	}

}
